package com.css.dzj.dzjap.modules.project.supervision.service.impl;

import com.css.dzj.dzjap.common.util.Constant;
import com.css.dzj.dzjap.common.util.ServiceUtil;
import com.css.dzj.dzjap.common.util.SnowflakeId;
import com.css.dzj.dzjap.modules.project.supervision.entity.QSupervisionChild;
import com.css.dzj.dzjap.modules.project.supervision.entity.SupervisionChild;
import com.css.dzj.dzjap.modules.project.supervision.mapper.SupervisionChildMapper;
import com.css.dzj.dzjap.modules.project.supervision.service.SupervisionChildService;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Service
public class SupervisionChildServiceImpl implements SupervisionChildService {
    @Autowired
    private SupervisionChildMapper supervisionChildMapper;
    @Autowired
    private JPAQueryFactory jpaQueryFactory;
    @Autowired
    private HttpServletRequest request;

    @Override
    @Transactional
    public void save(List<SupervisionChild> SupervisionChildList) {
        if (SupervisionChildList == null) {
            throw new RuntimeException("子表为空");
        }
        for (SupervisionChild supervisionChild : SupervisionChildList) {
            supervisionChild.setId(SnowflakeId.getId());
            // 设置默认值
            supervisionChild.setCreatPerson(ServiceUtil.getLoginName(request));
            supervisionChild.setCreatTime(new Date());
            supervisionChild.setDelFlag(Constant.NO_DELETE);
            supervisionChildMapper.save(supervisionChild);
        }
    }

    @Override
    @Transactional
    public void update(List<SupervisionChild> SupervisionChildList) {
        if (SupervisionChildList == null) {
            throw new RuntimeException("子表为空");
        }
        for (SupervisionChild supervisionChild : SupervisionChildList) {
            // 修改必须有ID
            if (StringUtils.isEmpty(supervisionChild.getId())){
                throw new RuntimeException("子表ID为空");
            }
            supervisionChildMapper.save(supervisionChild);
        }
    }

    @Override
    @Transactional
    public void logicDel(String parentId) {
        QSupervisionChild q = QSupervisionChild.supervisionChild;
        jpaQueryFactory.update(q)
                .set(q.delFlag, Constant.DELETE)
                .where(q.parentId.eq(parentId), q.delFlag.ne(Constant.DELETE));
    }

    @Override
    public List<SupervisionChild> queryByParentId(String parentId) {
        if (StringUtils.isEmpty(parentId)) {
            throw new RuntimeException("父ID为空");
        }
        QSupervisionChild q = QSupervisionChild.supervisionChild;
        return jpaQueryFactory.selectFrom(q)
                .where(q.parentId.eq(parentId), q.delFlag.eq(Constant.NO_DELETE))
                .fetch();
    }
}
